package com.hsogoo.study.spring.beandefinition;

import java.util.Arrays;

import org.junit.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry;
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;

import com.hsogoo.study.spring.resource.ResourceLoaderBean;

/**
 * Created by za-huangsaigang on 2018/9/17.
 */
public class BeanDefinitionTest {

    @Test
    public void testBeanDefinitionRegistry(){
        //实例化SimpleBeanDefinitionRegistry
        SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();

        //注册两个BeanDefinition
        BeanDefinition definition_1 = new GenericBeanDefinition();
        registry.registerBeanDefinition("d1", definition_1);

        BeanDefinition definition_2 = new RootBeanDefinition();
        registry.registerBeanDefinition("d2", definition_2);

        //方法测试
        System.out.println(registry.containsBeanDefinition("d1"));//true
        System.out.println(registry.getBeanDefinitionCount());//2
        System.out.println(Arrays.toString(registry.getBeanDefinitionNames()));//[d1, d2]
    }

    @Test
    public void testPropertiesBeanDefinitionReader(){
        BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
        //使用BeanDefinitionRegistry实例 构造BeanDefinitionReader
        PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(registry);
        //载入文件
        reader.loadBeanDefinitions("config.properties");
        System.out.println(registry.getBeanDefinition("mybean"));
        
    }

    @Test
    public void testAnnotatedBeanDefinitionReader(){
        SimpleBeanDefinitionRegistry registry= new SimpleBeanDefinitionRegistry();
        AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(registry);
        reader.register(ResourceLoaderBean.class);
        System.out.println(registry.getBeanDefinitionCount());
    }
}
